package cn.tedu._02notice.controller;

import cn.tedu._02notice.base.response.JsonResult;
import cn.tedu._02notice.base.response.StatusCode;
import cn.tedu._02notice.mapper.NoticeMapper;
import cn.tedu._02notice.pojo.dto.NoticeAddParam;
import cn.tedu._02notice.pojo.dto.NoticeListQuery;
import cn.tedu._02notice.pojo.dto.NoticeUpdateParam;
import cn.tedu._02notice.pojo.entity.Notice;
import cn.tedu._02notice.pojo.vo.NoticeDetailInfoVO;
import cn.tedu._02notice.pojo.vo.NoticeListVO;
import com.fasterxml.jackson.databind.util.BeanUtil;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Date;
import java.util.List;

/**
 * RestController注解：
 * 1.组合注解，等价于Controller注解+@ResponseBody注解；
 * 2.加完该注解后,所有的控制器方法上无需再添加@ResponseBody注解了;
 *
 * Slf4j注解:
 *   1.Lombok提供的日志注解;
 * Knife4j注解:
 *   1.Api(tags="")注解:控制器类名在Knife4j文档中的显示名称;
 *   2.ApiOperation("")注解:控制器方法名在Knife4j文档中的显示名称;
 *   3.ApiOperationSupport(order=10)注解:控制器方法在Knife4j文档中的显示顺序;
 *   4.ApiModelProperty(value="",required=true,example="")注解:POJO类属性描述;
 *   5.ApiImplicitParam(name="参数名", value="",required=true,example="",dataType="数据类型")
 *     声明参数方式接收的单个参数的描述;
 *   6.ApiImplicitParams(value={
 *        @ApiImplicitParam(...),
 *        @ApiImplicitParam(...)
 *  })
 *   7.@ApiIgnore注解:添加在方法的参数上，用于表示API文档框架应该忽略此参数
 */
@Slf4j
@RestController
@RequestMapping("/v1/notice/")
@Api(tags = "01.通知管理模块")
//@Controller
public class NoticeController {
    //自动装配
    @Autowired
    public NoticeMapper noticeMapper;
    /**
     * 1.新增公告功能
     */
    @ApiOperation("新增公告")
    @PostMapping("add")
    @ApiOperationSupport(order = 10)
    //@PostMapping("/v1/notice/add")
    //@ResponseBody
    public JsonResult addNotice(NoticeAddParam noticeAddParam){
        /*
             梳理业务逻辑，确定接口；
             1.接收客户端传递的数据；
             2.校验数据的合法性；
             3.调用mapper层插入数据[INSERT接口]；
             4.返回响应

             以注册用户业务为例：
             1.接收数据
             2.校验数据合法性
             3.确定用户名是否被占用[SELECT接口]
                3.1 被占用：return结束
                3.2 未被占用：插入数据[INSERT接口]，返回


              完成一个功能的流程步骤：
              1.确定是否需要DTO或者VO
              2.梳理业务逻辑，确定mapper层接口，完成mapper层接口
              3.完成Controller中的业务逻辑
              4.重启工程测试

         */
        log.debug("noticeAddParam"+noticeAddParam);
        Notice notice = new Notice();
        //赋值属性
        BeanUtils.copyProperties(noticeAddParam,notice);
        notice.setUserId(1L);
        notice.setCreateTime(new Date());
        notice.setUpdateTime(new Date());

        //调用mapper层接口
        log.debug("notice:" +notice);
        noticeMapper.insertNotice(notice);

        //return new JsonResult(StatusCode.OPERATION_SUCCESS);
        return JsonResult.success();
    }


    /**
     * 2.公告列表功能
     */
    @GetMapping("list")
    @ApiOperation("公告列表")
    @ApiOperationSupport(order = 20)
    //@GetMapping("/v1/notice/list")
    //@ResponseBody
    public JsonResult noticeList(@ApiIgnore NoticeListQuery noticeListQuery){
        log.debug("noticeListQuery"+noticeListQuery);
        String title = noticeListQuery.getTitle();
        Integer type = noticeListQuery.getType();

        //直接调用mapper层接口
        log.debug("noticeListQuery:" +noticeListQuery);
        List<NoticeListVO> noticeListVOS = noticeMapper.selectNotice(title, type);

        //return new JsonResult(StatusCode.OPERATION_SUCCESS,noticeListVOS);
        return JsonResult.success(noticeListVOS);
    }

    /**
     * 3.公告详情功能
     */

    @GetMapping("detail")
    @ApiOperation("公告详情")
    @ApiOperationSupport(order = 30)
    //@GetMapping("/v1/notice/detail")
    //@ResponseBody
    public JsonResult noticeDetailInfoVO(Long id){
        log.debug("id = "+ id);
        NoticeDetailInfoVO noticeDetailInfoVO = noticeMapper.detail(id);

        //return new JsonResult(StatusCode.OPERATION_SUCCESS,noticeDetailInfoVO);
        return JsonResult.success(noticeDetailInfoVO);
    }


    /**
     * 4.删除公告功能
     */
    @PostMapping("del")
    @ApiOperation("删除公告")
    @ApiOperationSupport(order = 40)

    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "id",value = "通知编号",required = true,dataType = "Long"),
            @ApiImplicitParam(name = "title",value = "通知标题",required = true,dataType = "String")
    })

    //@PostMapping("/v1/notice/del")
    //@ResponseBody
    public JsonResult delNotice(Long id,String title){  //title无任何作用，只是用来做Knife4j的测试
        log.debug("id = " + id);
        noticeMapper.delNotice(id);

        //return new JsonResult(StatusCode.OPERATION_SUCCESS);
        return JsonResult.success();
    }

    /**
     * 5.更新公告功能
     */
    @PostMapping("update")
    @ApiOperation("更新公告")
    @ApiOperationSupport(order = 50)
    //@PostMapping("/v1/notice/update")
    //@ResponseBody
    public JsonResult Notice(NoticeUpdateParam noticeUpdateParam){
        log.debug("noticeUpdateParam" + noticeUpdateParam);
        Notice notice = new Notice();
        BeanUtils.copyProperties(noticeUpdateParam,notice);
        notice.setUpdateTime(new Date());

        log.debug("notice:"+notice);
        noticeMapper.updateNotice(notice);

        //return new JsonResult(StatusCode.OPERATION_SUCCESS);
        return JsonResult.success();
    }
}
